from .language_model import LanguageModel
from openai import OpenAI

class OpenAILanguageModel(LanguageModel):
    def __init__(self, credentials):
        self.client = OpenAI(api_key=credentials)

    def generate(self, messages):
        completion =  self.client.chat.completions.create(
            model="gpt-4-1106-preview",
            messages=messages,
            stream=True
        )

        for chunk in completion:
            if chunk.choices and chunk.choices[0].delta:
                delta = chunk.choices[0].delta
                message_content = delta.content
                if message_content:
                    yield message_content
